<?php
if(defined("_PARA_QUOTE"))
    return;
define("_PARA_QUOTE", 1);

class Quote extends HTMLable {
    public $id;
    public $quote;
    public $name;
    public $createdate;
    public $active;


    public function Quote( $id, $quote, $name, $createdate, $active ) {
        $this->id         = $id;
        $this->quote      = $quote;
        $this->name       = $name;
        $this->createdate = $createdate;
        $this->active     = $active;
    }

    public function htmlize( $options ) {
        echo 
            "<div class='quote'>\n",
            "<span class='quote_quote'>", htmlspecialchars( $this->quote ), "</span><br/>\n",
            "<span class='quote_name'>", htmlspecialchars( $this->name ), "</span>\n",
            "</div>\n";
    }

}


class QuoteManager extends HTMLable {
    
    private $quotes = null;
    private $quotecount = 0;
    private $quotecount_wanted = 5;


    public function QuoteManager(  ) {
        global $PARAPARA;
        $ir = $PARAPARA->config_get('QUOTES', 'IN_ROTATION');
        if( !$ir )
            $ir = 5;

        $this->quotecount_wanted = $ir;
    }


    public function fetch_quotes() {
        global $PARAPARA;
        $dbl = $PARAPARA->dblayer;
        
        $this->quotes = $dbl->get_latest_quotes( $this->quotecount_wanted );
        if( is_array($this->quotes) )
            $this->quotecount = count( $this->quotes );
    }


    public function htmlize( $options ) {

        $quote = $this->choose_quote();
        echo 
            "<div id='quote_container'>\n";
        if( $quote )
            $quote->htmlize( array() );
        echo "</div>";
    }


    private function choose_quote() {
        if(!$this->quotes)
            $this->fetch_quotes();

        if($this->quotecount == 0 )
            return null;

        return $this->quotes[rand(0, $this->quotecount - 1 )];
    }
}

?>